﻿// The Nova Project by Ken Beckett.
// Copyright (C) 2007-2012 Inevitable Software, all rights reserved.
// Released under the Common Development and Distribution License, CDDL-1.0: http://opensource.org/licenses/cddl1.php

using System.Collections.Generic;
using System.Windows.Media;

using Nova.CodeDOM;

namespace Nova.UI
{
    /// <summary>
    /// The view model for a <see cref="CodeDOM.ConditionalDirective"/>.
    /// </summary>
    public abstract class ConditionalDirectiveVM : ConditionalDirectiveBaseVM
    {
        #region /* FIELDS */

        protected double _skippedTextOffset;
        protected double _skippedTextWidth;

        #endregion

        #region /* CONSTRUCTORS */

        /// <summary>
        /// Create a view model instance for the specified <see cref="CodeDOM.ConditionalDirective"/>.
        /// </summary>
        protected ConditionalDirectiveVM(ConditionalDirective conditionalDirective, Dictionary<CodeObject, CodeObjectVM> dictionary)
            : base(conditionalDirective, dictionary)
        { }

        #endregion

        #region /* PROPERTIES */

        /// <summary>
        /// The underlying <see cref="CodeDOM.ConditionalDirective"/> model.
        /// </summary>
        public ConditionalDirective ConditionalDirective
        {
            get { return (ConditionalDirective)CodeObject; }
        }

        #endregion

        #region /* RENDERING */

        protected override void RenderSkippedText(CodeRenderer renderer, RenderFlags flags)
        {
            if (!string.IsNullOrEmpty(ConditionalDirective.SkippedText) && !flags.HasFlag(RenderFlags.Description))
            {
                bool isPrefix = flags.HasFlag(RenderFlags.IsPrefix);
                if (!isPrefix)
                    renderer.NewLine();

                // Handle the special case of the skipped text having it's own border.
                // If measuring, save the offset and width of the skipped text, and include them in the total height/width.
                // If rendering, adjust the Y/height/width values first, then replace them when done.
                double height = Height, width = Width, y = Y;
                if (renderer.Measuring)
                    _skippedTextOffset = height;
                else
                {
                    Y += _skippedTextOffset;
                    Height -= _skippedTextOffset;
                    Width = _skippedTextWidth;
                }

                CreateBorder(renderer, flags, Brushes.Transparent, Brushes.Transparent);
                renderer.RenderText(ConditionalDirective.SkippedText, REDUNDANT_BRUSH, this);
                renderer.ReturnToBorderParent(this);

                if (renderer.Measuring)
                {
                    Height += height;
                    _skippedTextWidth = Width;
                    if (Width < width)
                        Width = width;
                }
                else
                {
                    Y = y;
                    Height = height;
                    Width = width;
                }

                if (isPrefix)
                    renderer.NewLine();
            }
        }

        #endregion
    }
}
